package xyz.quanguanjia.masterboy.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

import xyz.quanguanjia.masterboy.MBDApplication;
import xyz.quanguanjia.masterboy.MBDUserCenter;
import xyz.quanguanjia.masterboy.MineCellItemModel;
import xyz.quanguanjia.masterboy.R;

public class SettingListAdapter extends RecyclerView.Adapter {
    private List<MineCellItemModel> models;
    public SettingListAdapter() {
        List<MineCellItemModel> ms = new ArrayList<MineCellItemModel>();
        MineCellItemModel model1 = new MineCellItemModel();
        model1.title = "关于";
        model1.hasMore = true;
        model1.image = "ico_info";
        ms.add(model1);

        if (MBDUserCenter.shared().userInfoFetched()) {
            MineCellItemModel model2 = new MineCellItemModel();
            model2.title = "退出登录";
            model2.image = "logout";
            model2.hasMore = false;
            ms.add(model2);

            MineCellItemModel model3 = new MineCellItemModel();
            model3.title = "注销账号";
            model3.image = "ico_writeoff";
            model3.hasMore = false;
            ms.add(model3);
        }

        this.models = ms;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // 创建列表项视图
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_cell_setting, parent, false);
        return new MBDMineViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        // 绑定数据到列表项视图
        MineCellItemModel detail = this.models.get(position);
        ((MBDMineViewHolder)holder).bind(detail);
    }

    @Override
    public int getItemCount() {
        return this.models.size();
    }

    public static class MBDMineViewHolder extends RecyclerView.ViewHolder {
        private TextView titleTextView;
        private ImageView settingIco;

        private ImageView rightArrowImageView;

        public MBDMineViewHolder(@NonNull View itemView) {
            super(itemView);
            titleTextView = itemView.findViewById(R.id.text_setting_title);
            settingIco = itemView.findViewById(R.id.image_setting_ico);
            rightArrowImageView = itemView.findViewById(R.id.image_right_arrow);
        }

        public void bind(MineCellItemModel detail) {
            // 设置列表项视图的数据
            titleTextView.setText(detail.title);

            int resId = MBDApplication.getContext().getResources().getIdentifier(detail.image, "drawable", MBDApplication.getContext().getPackageName());
            settingIco.setImageResource(resId);

            rightArrowImageView.setVisibility(detail.hasMore ? View.VISIBLE : View.GONE);

        }
    }
}
